home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000…tember: Reference Library / Dev.CD Sep 00 RL Disk 1.toast / mac / Technical Documentation / Develop / develop Issue 28 / develop Issue 28 code / MacApp Debugging / IconEdit tests / IconEdit / UIconEdit.cp < prev    next >
Encoding:
Text File  |  1996-08-25  |  75.8 KB  |  2,271 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UIconEdit.cp
  3. // copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UICONEDIT__
  7. #include "UIconEdit.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __TOOLBOX__
  13. #include "Toolbox.h"
  14. #endif
  15.  
  16. #ifndef __UCLIPBOARDMGR__
  17. #include "UClipboardMgr.h"
  18. #endif
  19.  
  20. #ifndef __UMACAPPGLOBALS__
  21. #include "UMacAppGlobals.h"
  22. #endif
  23.  
  24. #ifndef __UMACAPPUTILITIES__
  25. #include "UMacAppUtilities.h"
  26. #endif
  27.  
  28. #ifndef __UMEMORY__
  29. #include "UMemory.h"
  30. #endif
  31.  
  32. #ifndef __UMENUMGR__
  33. #include "UMenuMgr.h"
  34. #endif
  35.  
  36. #ifndef __UPRINTING__
  37. #include "UPrinting.h"
  38. #endif
  39.  
  40. #ifndef __USCRIPTING__
  41. #include "UScripting.h"
  42. #endif
  43.  
  44. #ifndef __USTREAM__
  45. #include "UStream.h"
  46. #endif
  47.  
  48. #ifndef __UVIEWSERVER__
  49. #include "UViewServer.h"
  50. #endif
  51.  
  52. #ifndef __UWINDOW__
  53. #include "UWindow.h"
  54. #endif
  55.  
  56. // Toolbox
  57.  
  58. #ifndef __AEREGISTRY__
  59. #include <AERegistry.h>
  60. #endif
  61.  
  62. #ifndef __ICONS__
  63. #include <Icons.h>
  64. #endif
  65.  
  66. #ifndef __PICKER__
  67. #include <Picker.h>
  68. #endif
  69.  
  70. #ifndef __TOOLUTILS__
  71. #include <ToolUtils.h>
  72. #endif
  73.  
  74. // ANSI
  75.  
  76. #ifndef __STDIO__
  77. #include <stdio.h>
  78. #endif
  79.  
  80. #ifndef __STDLIB__
  81. #include <stdlib.h>
  82. #endif
  83.  
  84. //----------------------------------------------------------------------------------------
  85. // Constants
  86.  
  87. const short kIconHBits =            32;            // Number of horizontal bits in a bitmap.
  88. const short kIconVBits =            32;            // Number of vertical bits in a bitmap.
  89.  
  90. const short kIconSizeInBytes =        128;        // Number of bytes in an bitmap.
  91. const short kIconSizeInLongs =        32;            // Number of long words in a bitmap.
  92.     
  93. // Resource identifiers
  94.  
  95. const short kSeedIconId =            1000;        // Id of the seed icon resource.
  96. const short kIconWindowId =            1000;        // Id of the icon window 'view' resource.
  97. const short kIconEditViewId =        1001;        // Id of the TIconEditView resource.
  98. const short kPencilCursorId =        1000;        // Id of the pencil cursor resource.
  99.     
  100. // Constants for TIconEditView
  101.  
  102. const short kDefaultMagnification =    7;            // Default icon magnification.
  103. const short kBorder =                5;            // Border in which to inset drawing.
  104. const ResType kIconClipType =        'ICON';        // Our private clipboard data type.
  105.  
  106. // Command Numbers
  107.  
  108. const CommandNumber cZoomIn =            1000;    // Zoom In menu command.
  109. const CommandNumber cZoomOut =            1001;    // Zoom Out menu command.
  110. const CommandNumber cInvert =            1002;    // Invert menu command.
  111. const CommandNumber cDrawCommand =        1003;    // For drawing in the icon edit view.
  112. const CommandNumber cDrawPointsCommand = 1004;    // set point apple event
  113. const CommandNumber cSetColor =            1005;    // set a new document color
  114.  
  115.  
  116. short gSpecialDocCount;    //Added by CKopala 8/24/96
  117. //========================================================================================
  118. // CLASS TIconEditApplication
  119. //========================================================================================
  120. #undef Inherited
  121. #define Inherited TApplication
  122.  
  123. #pragma segment AInit
  124. MA_DEFINE_CLASS_M1(TIconEditApplication, Inherited);
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // TIconEditApplication::IIconEditApplication: 
  128. //----------------------------------------------------------------------------------------
  129. #pragma segment AInit
  130.  
  131. void TIconEditApplication::IIconEditApplication()
  132. {
  133.     this->IApplication(kFileType, kSignature);
  134.  
  135.     MA_REGISTER_CLASS(TIconEditView);
  136.  
  137. gSpecialDocCount = 0;    //Added by CKopala 8/24/96    
  138. } // TIconEditApplication::IIconEditApplication 
  139.  
  140. //----------------------------------------------------------------------------------------
  141. // TIconEditApplication destructor
  142. //----------------------------------------------------------------------------------------
  143. #pragma segment MADestructorRes
  144.  
  145. TIconEditApplication::~TIconEditApplication()
  146. {
  147. }
  148.  
  149. //----------------------------------------------------------------------------------------
  150. // TIconEditApplication::DoMakeDocument: 
  151. //----------------------------------------------------------------------------------------
  152. #pragma segment AOpen
  153.  
  154. TDocument *TIconEditApplication::DoMakeDocument(CommandNumber    /*itsCommandNumber*/,
  155.                                                               TFile*        itsFile)
  156. {
  157.     TIconDocument* anIconDocument;
  158. if (gSpecialDocCount >= 6)        //Added by Ckopala 8/24/96
  159.      Failure(memFullErr, 0);        //Added by Ckopala 8/24/96
  160.     
  161.     anIconDocument = new TIconDocument;            // Create a TIconDocument object.
  162.     anIconDocument->IIconDocument(itsFile);        // Initialize it. 
  163.  
  164.     return anIconDocument;                        // Return a reference to the document.
  165. } // TIconEditApplication::DoMakeDocument 
  166.  
  167. //----------------------------------------------------------------------------------------
  168. // TIconEditApplication::GetContainedObject: 
  169. //----------------------------------------------------------------------------------------
  170. #pragma segment OSLDispatchRes
  171. MScriptableObject* TIconEditApplication::GetContainedObject(DescType desiredType,
  172.                                                     DescType selectionForm,const CAEDesc& selectionData)
  173. {
  174.  
  175.     MScriptableObject* result = NULL;
  176.     TDocument* theDocument = NULL;
  177.     
  178. if (desiredType == cDocument && selectionForm == formName)
  179.     {
  180. //Note: MacApp 3.3.1 TApplication::GetContainedObject dose not work if a script
  181. //attempts to get a document that does not exist. In the case where there are
  182. //no open documents, the result is an access fault. Therefore, I'm using the
  183. //following substitute code.
  184.         CStr255    theName;
  185.         selectionData.GetString(theName);        
  186.         CNoGhostDocsIterator    iter(this);
  187.         
  188.         for (TDocument* aDocument = iter.FirstDocument(); iter.More(); aDocument = iter.NextDocument())
  189.             {
  190.                 if (aDocument != NULL)
  191.                     {
  192.                         CStr255 name = gEmptyString;
  193.                         aDocument -> GetTitle(name);
  194.                         if (name == theName)
  195.                             {                    
  196.                                 theDocument = aDocument;
  197.                                 result = theDocument;
  198.                                 return result;
  199.                             }
  200.                     }
  201.             }
  202.     }
  203.     else
  204.         {
  205.             result = Inherited::GetContainedObject(desiredType, selectionForm, selectionData);
  206.         }
  207.     return result;
  208. }
  209. //----------------------------------------------------------------------------------------
  210. // TIconEditApplication::MakeViewForAlienClipboard: 
  211. //----------------------------------------------------------------------------------------
  212. #pragma segment AClipboard
  213.  
  214. TView* TIconEditApplication::MakeViewForAlienClipboard()
  215. {
  216.     long            offset;
  217.  
  218.     // Does the scrap contain my type?
  219.     if (GetScrap(NULL, kIconClipType, &offset) > 0)
  220.     {
  221.         TIconDocument*    clipDocument = NULL;
  222.         TIconEditView* clipView = NULL;
  223.  
  224.         // Need a document to represent the data to be put on the clipboard. 
  225.         clipDocument = new TIconDocument;
  226.         clipDocument->IIconDocument(NULL);
  227.  
  228.         FailInfo fi;
  229.         Try(fi)                                                // Install failure handler.
  230.         {
  231.             clipDocument->ReturnBitMap()->LoadFromScrap();    // Get 'ICON' data from scrap.
  232.  
  233.             clipView = new TIconEditView;
  234.             clipView->IIconEditView(clipDocument, NULL, gZeroVPt, 1);
  235.             
  236.             fi.Success();                                // Don't need failure handler
  237.                                                         // anymore.
  238.         }
  239.         else
  240.         {
  241.             clipDocument = (TIconDocument*)FreeIfObject(clipDocument);    // Free
  242.                                                                         // clipDocument
  243.             fi.ReSignal();
  244.         }
  245.         return clipView;        // Return view for Clipboard.
  246.     }
  247.     else
  248.         return Inherited::MakeViewForAlienClipboard();
  249. } // TIconEditApplication::MakeViewForAlienClipboard 
  250.  
  251.  
  252. //========================================================================================
  253. // CLASS TIconDocument
  254. //========================================================================================
  255. #undef Inherited
  256. #define Inherited TFileBasedDocument
  257.  
  258. #pragma segment AOpen
  259. MA_DEFINE_CLASS_M1(TIconDocument, Inherited);
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // TIconDocument constructor 
  263. //----------------------------------------------------------------------------------------
  264. #pragma segment AOpen
  265.  
  266. TIconDocument::TIconDocument()
  267. {
  268.     fIconBitMap = NULL;                            // Set this to NULL so that if IDocument
  269.                                                 // fails, TIconDocument.Free works okay.
  270.     fIconView = NULL;
  271.     fColor = gRGBBlack;                            // init drawing color to black
  272.  
  273. gSpecialDocCount++;    //Added by CKopala 8/24/96
  274. } // TIconDocument::TIconDocument 
  275.  
  276. //----------------------------------------------------------------------------------------
  277. // TIconDocument::IIconDocument: 
  278. //----------------------------------------------------------------------------------------
  279. #pragma segment AOpen
  280.  
  281. void TIconDocument::IIconDocument(TFile* itsFile)
  282. {
  283.     TIconBitMap* anIconBitMap;
  284.  
  285.     this->IFileBasedDocument(itsFile,            // This document's file 
  286.                                kSignature);        // This document's scrap type 
  287.  
  288.     FailInfo fi;                                // Install failure handler in case we
  289.     Try(fi)                                        // can't create the bit map object.
  290.     {
  291.         anIconBitMap = new TIconBitMap;            // Allocate a new icon bitmap.
  292.         anIconBitMap->IIconBitMap();            // Initialize it.
  293.         fIconBitMap = anIconBitMap;
  294.  
  295.         fi.Success();                            // Don't need failure handler anymore. 
  296.     }
  297.     else
  298.     {
  299.         this->Free();                                                // Free me too!
  300.         fi.ReSignal();
  301.     }
  302. } // TIconDocument::IIconDocument 
  303.  
  304.  
  305. //----------------------------------------------------------------------------------------
  306. // TIconDocument::Free: 
  307. //----------------------------------------------------------------------------------------
  308. #pragma segment AClose
  309. TIconDocument::~TIconDocument()
  310. {
  311.     fIconBitMap = (TIconBitMap*)FreeIfObject(fIconBitMap);// Dispose of the bitmap if
  312.                                                            // non-NULL.
  313. gSpecialDocCount--;    //Added by CKopala 8/24/96    
  314. } // TIconDocument::Free 
  315.  
  316.  
  317. //----------------------------------------------------------------------------------------
  318. // TIconDocument::DoInitialState: This method is called to set the document's data to the
  319. // "new" state, as when the user chooses to open a new document instead of an existing
  320. // one. We set the value of the document's icon bit map to that of a "seed" icon in our
  321. // resource file. That way we can the document's initial state simply by changing the
  322. // "seed" icon resource.
  323. //----------------------------------------------------------------------------------------
  324. #pragma segment AOpen
  325.  
  326. void TIconDocument::DoInitialState()        // OVERRIDE
  327. {
  328.     Handle seedIcon;
  329.  
  330.     seedIcon = GetIcon(kSeedIconId);            // Get the seed icon resource
  331.     if (seedIcon != NULL)                        // If we got the seed icon resource
  332.         fIconBitMap->SetIconBitMap(seedIcon);
  333. #if qDebug
  334.     else
  335.         ProgramBreak("Unable to get the seed icon resource.");
  336. #endif
  337.  
  338. } // TIconDocument::DoInitialState 
  339.  
  340.  
  341. //----------------------------------------------------------------------------------------
  342. // TIconDocument::DoMakeViews: 
  343. //----------------------------------------------------------------------------------------
  344. #pragma segment AOpen
  345.  
  346. void TIconDocument::DoMakeViews(Boolean forPrinting)// OVERRIDE
  347. {
  348.     TWindow*            aWindow;
  349.     TIconEditView*         iconView;
  350.     TStdPrintHandler*    aPrintHandler;
  351.  
  352.     if (forPrinting)                            // If for printing, only need the view.
  353.     {
  354.         iconView = (TIconEditView *)gViewServer->DoCreateViews(this, NULL, kIconEditViewId, gZeroVPt);//new
  355.         FailNIL(iconView);
  356.     }
  357.     else
  358.     {                                            // Otherwise need view and window.
  359.         FailNIL(aWindow = gViewServer->NewTemplateWindow(kIconWindowId, this));
  360.         iconView = (TIconEditView*)(aWindow->FindSubView('ICON'));    // Get reference to
  361.                                                                     // the view.
  362.     }
  363.  
  364.     fIconView = iconView;
  365.     
  366.     aPrintHandler = new TStdPrintHandler;                // Create a print handler.
  367.     aPrintHandler->IStdPrintHandler(this,                // The document.
  368.                                     iconView,            // The view to be printed.
  369.                                     !kSquareDots, 
  370.                                     kFixedSize,            // Horzontal page size is fixed.
  371.                                     kFixedSize);        // Vertical page size is fixed.
  372. } // TIconDocument::DoMakeViews 
  373.  
  374.  
  375. //----------------------------------------------------------------------------------------
  376. // TIconDocument::DoMenuCommand: 
  377. //----------------------------------------------------------------------------------------
  378. #pragma segment ASelCommand
  379.  
  380. void TIconDocument::DoMenuCommand (CommandNumber aCommandNumber) 
  381. {
  382.  
  383.     switch (aCommandNumber)
  384.         {
  385.         case cInvert:
  386.                 TInvertCommand *anInvertCommand = new TInvertCommand;
  387.                 anInvertCommand->IInvertCommand(this);
  388.                 anInvertCommand->fUseAppleEvent = TRUE;
  389.                 PostCommand(anInvertCommand);
  390.             break; 
  391.             
  392.         case cZoomIn:
  393.                 TZoomInCommand * theZoomInCommand = new TZoomInCommand();
  394.                 theZoomInCommand->IZoomInCommand(this);
  395.                 theZoomInCommand->fUseAppleEvent = TRUE;
  396.                 this->PostCommand(theZoomInCommand);
  397.             break;
  398.         
  399.         case cZoomOut:
  400.                 TZoomOutCommand * theZoomOutCommand = new TZoomOutCommand();
  401.                 theZoomOutCommand->IZoomOutCommand(this);
  402.                 theZoomOutCommand->fUseAppleEvent = TRUE;
  403.                 this->PostCommand(theZoomOutCommand);
  404.             break;
  405.             
  406.         case cSetColor:
  407.             {
  408.                 CRGBColor    newColor;
  409.                 CStr255        thePrompt = "Pick a new color";
  410.                 if (GetColor(kBestSystemLocation, thePrompt, fColor, newColor))
  411.                 {
  412.                     if (TOSADispatcher::fgDispatcher->GetDefaultTarget()->IsRecordingOn())
  413.                     {
  414.                         TSetPropertyEvent *appleEvent = new TSetPropertyEvent;
  415.                         appleEvent->ISetPropertyEvent(gServerAddress, kAENoReply, this, pColor);
  416.                         CTempDesc theNewColor;
  417.                         theNewColor.PutRGBColor(newColor);
  418.                         appleEvent->WriteParameter(keyAEData, theNewColor);
  419.                         appleEvent->Send();
  420.                     }
  421.                     else
  422.                     {
  423.                         TSetColorCommand *aSetColorCommand = new TSetColorCommand();
  424.                         aSetColorCommand->ISetColorCommand(this, newColor);
  425.                         PostCommand(aSetColorCommand);
  426.                     }
  427.                 }
  428.             }
  429.             break;
  430.             
  431.         default:
  432.             Inherited::DoMenuCommand(aCommandNumber);  
  433.             break;  
  434.         }
  435. } // TIconDocument::DoMenuCommand             
  436.             
  437. //----------------------------------------------------------------------------------------
  438. // TIconDocument::DoScriptCommand    (OVERRIDE)
  439. //----------------------------------------------------------------------------------------
  440.  
  441. void TIconDocument::DoScriptCommand(CommandNumber aCommand,
  442.                                     TAppleEvent* message,
  443.                                     TAppleEvent* reply)
  444. {
  445.     switch (aCommand)
  446.     {
  447.         case cInvert:
  448.             {
  449.                 TInvertCommand *anInvertCommand = new TInvertCommand;
  450.                 anInvertCommand->IInvertCommand(this);
  451.                 PostCommand(anInvertCommand);
  452.             }
  453.             break;
  454.             
  455.         case cZoomIn:
  456.                 TZoomInCommand * theZoomInCommand = new TZoomInCommand();
  457.                 theZoomInCommand->IZoomInCommand(this);
  458.                 this->PostCommand(theZoomInCommand);
  459.             break;
  460.             
  461.         case cZoomOut:
  462.                 TZoomOutCommand * theZoomOutCommand = new TZoomOutCommand();
  463.                 theZoomOutCommand->IZoomOutCommand(this);
  464.                 this->PostCommand(theZoomOutCommand);
  465.             break;
  466.             
  467.         case cDrawPointsCommand:
  468.             {
  469.                 TDrawPointsCommand *drawPointsCommand = new TDrawPointsCommand;
  470.                 drawPointsCommand->IDrawPointsCommand(this, message);
  471.                 PostCommand(drawPointsCommand);
  472.             }
  473.             break;
  474.         
  475.         default:
  476.             Inherited::DoScriptCommand(aCommand, message, reply);
  477.             break;
  478.     }
  479. } // TIconDocument::DoScriptCommand
  480.  
  481. TIconEditView* TIconDocument::GetIconView()
  482. {
  483.   return  fIconView;
  484. }
  485.  
  486. //----------------------------------------------------------------------------------------
  487. // TIconDocument::DoNeedDiskSpace: 
  488. //----------------------------------------------------------------------------------------
  489. #pragma segment AWriteFile
  490.  
  491. void TIconDocument::DoNeedDiskSpace(TFile*    itsFile,
  492.                                            long&    dataForkBytes,
  493.                                            long&    rsrcForkBytes)//new
  494. {
  495.     Inherited::DoNeedDiskSpace(    itsFile, 
  496.                                 dataForkBytes,// In case parent class saves data.
  497.                                 rsrcForkBytes);
  498.  
  499.     dataForkBytes = dataForkBytes +                // Add the size of the icon in bytes and
  500.                       kIconSizeInBytes +            // the space taken up by the color information
  501.                     sizeof(CRGBColor);            // …the number of bytes in the data fork.
  502. } // TIconDocument::DoNeedDiskSpace 
  503.  
  504.  
  505. //----------------------------------------------------------------------------------------
  506. // TIconDocument::DoRead: 
  507. //----------------------------------------------------------------------------------------
  508. #pragma segment AReadFile
  509.  
  510. void TIconDocument::DoRead(TFile*    itsFile,
  511.                              Boolean    forPrinting)
  512. {
  513.     Inherited::DoRead(itsFile, forPrinting);    // In case parent class reads data. 
  514.  
  515.     TFileStream* theFileStream = new TFileStream;
  516.     
  517.     theFileStream->IFileStream(itsFile);
  518.     fIconBitMap->ReadFrom(theFileStream);        // Read Data from the file system into 
  519.                                                 // …the icon and handle any errors.
  520.                                                 
  521.     theFileStream->ReadBytes(fColor, sizeof(CRGBColor)); // read the color from the stream
  522.     
  523.     theFileStream->Free();
  524. } // TIconDocument::DoRead 
  525.  
  526.  
  527. //----------------------------------------------------------------------------------------
  528. // TIconDocument::DoSetupMenus: 
  529. //----------------------------------------------------------------------------------------
  530. #pragma segment ARes
  531.  
  532. void TIconDocument::DoSetupMenus()
  533. {
  534.     Inherited::DoSetupMenus();                    // Set up inherited menus.
  535.     Enable(cInvert, TRUE);                        // The icon can always be inverted.
  536.     Enable(cSetColor, TRUE);                    // The color can always be set
  537. } // TIconDocument::DoSetupMenus 
  538.  
  539.  
  540. //----------------------------------------------------------------------------------------
  541. // TIconDocument::DoWrite: 
  542. //----------------------------------------------------------------------------------------
  543. #pragma segment AWriteFile
  544.  
  545. void TIconDocument::DoWrite(TFile*    itsFile,
  546.                                    Boolean    makingCopy)
  547. {
  548.     Inherited::DoWrite(itsFile, makingCopy);        // In case parent class writes data. 
  549.  
  550.     TFileStream* theFileStream = new TFileStream;
  551.     
  552.     theFileStream->IFileStream(itsFile);
  553.     fIconBitMap->WriteTo(theFileStream);            // Write data the icon to the file 
  554.                                                     // …and handle any errors.
  555.                                                     
  556.     theFileStream->WriteBytes(&fColor, sizeof(CRGBColor));
  557.     
  558.     theFileStream->Free();
  559. } // TIconDocument::DoWrite 
  560.  
  561. //----------------------------------------------------------------------------------------
  562. // TIconDocument::InvertIcon: 
  563. //----------------------------------------------------------------------------------------
  564. #pragma segment ADoCommand
  565.  
  566. void TIconDocument::InvertIcon()
  567. {
  568.     fIconBitMap->Invert();                        // Invert the bits of the icon.
  569.     this->RedrawViews();                    // Make sure all views get redrawn.
  570. } // TIconDocument::InvertIcon 
  571.  
  572.  
  573. //----------------------------------------------------------------------------------------
  574. // TIconDocument::ClearIcon: 
  575. //----------------------------------------------------------------------------------------
  576. #pragma segment ADoCommand
  577.  
  578. void TIconDocument::ClearIcon()
  579. {
  580.     fIconBitMap->Clear();                        // Clear the icon bits.
  581.     this->RedrawViews();                        // Make sure all views get redrawn.
  582. } // TIconDocument::ClearIcon 
  583.  
  584. //----------------------------------------------------------------------------------------
  585. // TIconDocument::SetIcon: 
  586. //----------------------------------------------------------------------------------------
  587. #pragma segment ADoCommand
  588.  
  589. void TIconDocument::SetIcon(TIconBitMap* newIcon)
  590. {
  591.     if (fIconBitMap != newIcon)
  592.     {
  593.         fIconBitMap = (TIconBitMap *)FreeIfObject(fIconBitMap);
  594.         fIconBitMap = newIcon;                        // Note this doesn't copy the icon.
  595.     }
  596.     this->RedrawViews();                            // Make sure all views get redrawn.
  597.  
  598. } // TIconDocument::SetIcon 
  599.  
  600. //----------------------------------------------------------------------------------------
  601. // TIconDocument::ReturnBitMap: 
  602. //----------------------------------------------------------------------------------------
  603. #pragma segment AClipboard
  604.  
  605. TIconBitMap* TIconDocument::ReturnBitMap()
  606. {
  607.     return fIconBitMap;
  608. } // TIconDocument::ReturnBitMap 
  609.  
  610.  
  611. //----------------------------------------------------------------------------------------
  612. // TIconDocument::RedrawViews: 
  613. //----------------------------------------------------------------------------------------
  614. #pragma segment ARes
  615.  
  616. void TIconDocument::RedrawViews()
  617. {
  618.     fIconView->ForceRedraw();
  619. } // TIconDocument::RedrawViews 
  620.  
  621. //----------------------------------------------------------------------------------------
  622. // TIconDocument::GetSetPropertyInfo:     {OVERRIDE}
  623. //----------------------------------------------------------------------------------------
  624. #pragma segment ARes
  625.  
  626. void TIconDocument::GetSetPropertyInfo(    DescType        whichProperty,
  627.                                         CommandNumber&    cmdNum,
  628.                                         Boolean&        canUndo,
  629.                                         Boolean&        causesChange,
  630.                                         TCommandHandler* &theContext)
  631. {
  632.     switch(whichProperty)
  633.     {
  634.         case pColor:
  635.             cmdNum = cSetColor;
  636.             canUndo = TRUE;
  637.             causesChange = TRUE;
  638.             theContext = this;
  639.             break;
  640.         
  641.         default:
  642.             Inherited::GetSetPropertyInfo(whichProperty, cmdNum, canUndo, causesChange, theContext);
  643.             break;
  644.     }
  645. } // TIconDocument::GetSetPropertyInfo
  646.  
  647.  
  648. //----------------------------------------------------------------------------------------
  649. // TIconDocument::GetObjectProperty:     {OVERRIDE}
  650. //----------------------------------------------------------------------------------------
  651. #pragma segment ARes
  652.  
  653. Boolean TIconDocument::GetObjectProperty(CAEDesc& thePropertyValue,
  654.                                          DescType whichProperty,
  655.                                          const CAEDesc& desiredType)
  656. {
  657.     Boolean hasProperty = TRUE;
  658.     
  659.     switch (whichProperty)
  660.     {
  661.         case pColor:
  662.             thePropertyValue.PutRGBColor(fColor);
  663.             break;
  664.             
  665.         default:
  666.             hasProperty = Inherited::GetObjectProperty(thePropertyValue, whichProperty, desiredType);
  667.             break;
  668.             
  669.     }
  670.     
  671.     return hasProperty;
  672. }
  673.  
  674. //----------------------------------------------------------------------------------------
  675. // TIconDocument::SetObjectProperty:     {OVERRIDE}
  676. //----------------------------------------------------------------------------------------
  677. #pragma segment ARes
  678.  
  679. void TIconDocument::SetObjectProperty(    const CAEDesc&    thePropertyValue,
  680.                                         DescType        whichProperty)
  681. {
  682.     switch(whichProperty)
  683.     {
  684.         case pColor:
  685.             thePropertyValue.GetRGBColor(fColor);
  686.             this->RedrawViews();
  687.             break;
  688.             
  689.         default:
  690.             Inherited::SetObjectProperty(thePropertyValue, whichProperty);
  691.             break;
  692.     }
  693. }
  694.                 
  695.     
  696.  
  697.  
  698. //----------------------------------------------------------------------------------------
  699. // TIconDocument::GetIconColor: Return the color of the bit map.
  700. //----------------------------------------------------------------------------------------
  701. #pragma segment ARes
  702.  
  703. CRGBColor TIconDocument::GetIconColor()
  704. {
  705.     return fColor;
  706. } // TIconDocument::GetIconColor
  707.  
  708. //----------------------------------------------------------------------------------------
  709. // TIconDocument::SetIconColor: Set the color of the icon bitmap.
  710. //----------------------------------------------------------------------------------------
  711. #pragma segment ARes
  712.  
  713. void TIconDocument::SetIconColor(CRGBColor& newColor)
  714. {
  715.     fColor = newColor;
  716.     this->RedrawViews();
  717. } // TIconDocument::SetIconColor
  718.  
  719. //========================================================================================
  720. // CLASS TIconEditView
  721. //========================================================================================
  722. #undef Inherited
  723. #define Inherited TView
  724.  
  725. #pragma segment AOpen
  726. MA_DEFINE_CLASS_M1(TIconEditView, Inherited);
  727.  
  728. //----------------------------------------------------------------------------------------
  729. // TIconEditView constructor 
  730. //----------------------------------------------------------------------------------------
  731. #pragma segment AOpen
  732.  
  733. TIconEditView::TIconEditView() 
  734. {
  735.     fMagnification = kDefaultMagnification;    
  736. } // TIconEditView::TIconEditView 
  737.  
  738. //----------------------------------------------------------------------------------------
  739. // TIconEditView destructor
  740. //----------------------------------------------------------------------------------------
  741. #pragma segment MADestructorRes
  742.  
  743. TIconEditView::~TIconEditView()
  744. {
  745. }
  746.  
  747.  
  748. //----------------------------------------------------------------------------------------
  749. // TIconEditView::IIconEditView: 
  750. //----------------------------------------------------------------------------------------
  751. #pragma segment AOpen
  752.  
  753. void TIconEditView::IIconEditView(TDocument* itsDocument,
  754.                                           TView* itsSuperView,
  755.                                           const VPoint& itsLocation,
  756.                                           short itsMagnification)
  757. {
  758.     this->IView(itsDocument, itsSuperView, itsLocation, gZeroVPt, sizeVariable, sizeVariable);
  759.     
  760.     fMagnification = itsMagnification;
  761.     fIconDocument = (TIconDocument*)itsDocument;
  762. } // TIconEditView::IIconEditView 
  763.  
  764. //----------------------------------------------------------------------------------------
  765. // TIconEditView::DoPostCreate: 
  766. //----------------------------------------------------------------------------------------
  767. #pragma segment AOpen
  768.  
  769. void TIconEditView::DoPostCreate(TDocument*    itsDocument)
  770. {
  771.     fIconDocument = (TIconDocument*)itsDocument;
  772. } // TIconEditView::DoPostCreate 
  773.  
  774.  
  775. //----------------------------------------------------------------------------------------
  776. // TIconEditView::CalcMinFrame: 
  777. //----------------------------------------------------------------------------------------
  778. #pragma segment ANonRes
  779.  
  780. void TIconEditView::CalcMinFrame(VRect& minFrame) 
  781. {
  782.     minFrame = VRect (  0,
  783.                         0,
  784.                         kIconHBits * fMagnification + kBorder + kBorder,
  785.                         kIconVBits * fMagnification + kBorder + kBorder);
  786. } // TIconEditView::CalcMinFrame 
  787.  
  788. //----------------------------------------------------------------------------------------
  789. // TIconEditView::ContainsClipType: 
  790. //----------------------------------------------------------------------------------------
  791. #pragma segment AClipboard
  792.  
  793. Boolean TIconEditView::ContainsClipType(ResType aType)
  794. {
  795.     return (aType == kIconClipType);
  796. } // TIconEditView::ContainsClipType 
  797.  
  798. //----------------------------------------------------------------------------------------
  799. // TIconEditView::DoMenuCommand: 
  800. //----------------------------------------------------------------------------------------
  801. #pragma segment ASelCommand
  802.  
  803. void TIconEditView::DoMenuCommand(CommandNumber aCommandNumber)
  804. {
  805.     TIconEditCommand*    anIconEditCommand;
  806.     TIconPasteCommand*    anIconPasteCommand;
  807.  
  808.     switch (aCommandNumber)                                // Decide if this command is ours…
  809.     {
  810.         case cCut:
  811.         case cCopy:
  812.         case cClear:
  813.             {                                            // Post a TIconEditCommand object.
  814.                 anIconEditCommand = new TIconEditCommand;
  815.                 anIconEditCommand->IIconEditCommand(aCommandNumber, this, fIconDocument);
  816.                 this->PostCommand(anIconEditCommand);
  817.                 return;
  818.             }
  819.  
  820.         case cPaste:
  821.             {                                            // Post a TIconPasteCommand object.
  822.                 anIconPasteCommand = new TIconPasteCommand;
  823.                 anIconPasteCommand->IIconPasteCommand(this, fIconDocument);
  824.                 this->PostCommand(anIconPasteCommand);
  825.                 return;
  826.             }
  827.  
  828.         default:                                    // Otherwise, let someone else handle it.
  829.             Inherited::DoMenuCommand(aCommandNumber);
  830.             return;
  831.     }
  832. } // TIconEditView::DoMenuCommand 
  833.  
  834.  
  835. //----------------------------------------------------------------------------------------
  836. // TIconEditView::DoKeyEvent: 
  837. //----------------------------------------------------------------------------------------
  838. #pragma segment ASelCommand
  839.  
  840. void TIconEditView::DoKeyEvent(TToolboxEvent* info)
  841. {
  842.     TIconEditCommand* anIconEditCommand;
  843.  
  844.     if (info->fCharacter == chBackspace)
  845.     {
  846.         // Post a TIconEditCommand object.
  847.         anIconEditCommand = new TIconEditCommand;
  848.         anIconEditCommand->IIconEditCommand(cClear, this, fIconDocument);    // Same as Clear menu command.
  849.         this->PostCommand(anIconEditCommand);
  850.         return;
  851.     }
  852.     Inherited::DoKeyEvent(info);                            // In case someone else wants it. 
  853. } // TIconEditView::DoKeyEvent 
  854.  
  855.  
  856. //----------------------------------------------------------------------------------------
  857. // TIconEditView::DoMouseCommand: 
  858. //----------------------------------------------------------------------------------------
  859. #pragma segment ASelCommand
  860.  
  861. void TIconEditView::DoMouseCommand(VPoint&            theMouse,
  862.                                           TToolboxEvent*    /*event*/ ,
  863.                                           CPoint            /*hysteresis*/)
  864. {
  865.     VPoint                iconBit;
  866.     TIconDrawCommand*    anIconDrawCommand;
  867.  
  868.     if (this->PointToBit(theMouse, iconBit))                    // If CPoint is within the icon
  869.     {
  870.         anIconDrawCommand = new TIconDrawCommand;                // …then make a drawing command.
  871.         anIconDrawCommand->IIconDrawCommand(this, fIconDocument, theMouse);
  872.         this->PostCommand(anIconDrawCommand);                    // and Post it.
  873.         return;
  874.     }
  875.     return;                                                        // …else take no action.  
  876. } // TIconEditView::DoMouseCommand 
  877.  
  878. //----------------------------------------------------------------------------------------
  879. // TIconEditView::DoSetCursor: 
  880. //----------------------------------------------------------------------------------------
  881. #pragma segment ARes
  882.  
  883. void TIconEditView::DoSetCursor(const VPoint&    localPoint,
  884.                                        RgnHandle        cursorRegion)//new
  885. {
  886.     VPoint        iconBit;
  887.     CursHandle    pencilCursor;
  888.  
  889.     CTemporaryRegion outerRegion;
  890.     CTemporaryRegion innerRegion;
  891.     
  892.     this->GetExtentRegion(outerRegion);
  893.     CopyRgn(outerRegion,innerRegion);
  894.     InsetRgn(innerRegion,kBorder,kBorder);
  895.  
  896.     if (this->PointToBit(localPoint, iconBit))            // If the cursor is not in the borders…
  897.     {
  898.         pencilCursor = GetCursor(this->GetCursorID());    // Attempt to get the pencil cursor.
  899.         FailNILResource((Handle)pencilCursor);
  900.         SetCursor(*pencilCursor);                        // Set the cursor on the screen.
  901.         CopyRgn(innerRegion,cursorRegion);
  902.     }
  903.     else
  904.     {
  905.         SetCursor(&(qd.arrow));                            // Set the cursor on the screen. 
  906.         DiffRgn(outerRegion,innerRegion,cursorRegion);
  907.     }
  908. } // TIconEditView::DoSetCursor 
  909.  
  910.  
  911. //----------------------------------------------------------------------------------------
  912. // TIconEditView::DoSetupMenus: 
  913. //----------------------------------------------------------------------------------------
  914. #pragma segment ARes
  915.  
  916. void TIconEditView::DoSetupMenus()
  917. {
  918.     Inherited::DoSetupMenus();                    // Set up inherited menus.
  919.  
  920.     Enable(cZoomIn, TRUE);                        // Can always zoom in.
  921.     Enable(cZoomOut, fMagnification > 1);        // Can zoom out if not at smallest size.
  922.     Enable(cSetColor, TRUE);                    // can always set the color
  923.     Enable(cCut, TRUE);                            // Always enable the Cut command.
  924.     Enable(cCopy, TRUE);                        // Always enable the Copy command.
  925.     Enable(cClear, TRUE);                        // Always enable the Clear command.
  926.     gClipboardMgr->CanPaste(kIconClipType);        // This view can paste 'ICON' data.
  927. } // TIconEditView::DoSetupMenus 
  928.  
  929.  
  930. //----------------------------------------------------------------------------------------
  931. // TIconEditView::Draw: 
  932. //----------------------------------------------------------------------------------------
  933. #pragma segment ARes
  934.  
  935. void TIconEditView::Draw (const VRect& /*area*/) //new
  936. {
  937.     CRect    drawingRect = CRect ( kBorder, 
  938.                                  kBorder, 
  939.                                  kBorder + (kIconHBits * fMagnification),
  940.                                  kBorder + (kIconVBits * fMagnification));
  941.     fIconDocument->ReturnBitMap()->Draw(drawingRect, fIconDocument->GetIconColor());
  942. } // TIconEditView::Draw 
  943.  
  944. //----------------------------------------------------------------------------------------
  945. // TIconEditView::DrawBit: 
  946. //----------------------------------------------------------------------------------------
  947. #pragma segment ARes
  948.  
  949. void TIconEditView::DrawBit (    VPoint theBit, 
  950.                                 Boolean turnBitOn, 
  951.                                 CRGBColor& drawingColor)
  952. {    
  953.     CRect    theRect = CRect ( kBorder + ((short) theBit.h) * fMagnification,
  954.                              kBorder + ((short) theBit.v) * fMagnification,
  955.                              kBorder + ((short) (theBit.h + 1)) * fMagnification,
  956.                              kBorder + ((short) (theBit.v + 1)) * fMagnification);
  957.                              // Setup the bit's CRect the in edit view.
  958.         
  959.     if (turnBitOn)
  960.     {
  961.         if (qNeedsColorQD || HasColorQD())
  962.         {
  963.             CRGBColor oldColor;
  964.             
  965.             GetIfColor(oldColor);
  966.             SetIfColor(drawingColor);
  967.             
  968.             PenNormal();
  969.             PaintRect(theRect);
  970.             SetIfColor(oldColor);
  971.         }
  972.         else
  973.             FillRect(theRect, &qd.black);
  974.     }
  975.     else
  976.         FillRect(theRect, &qd.white);
  977. } // TIconEditView::DrawBit 
  978.  
  979. //----------------------------------------------------------------------------------------
  980. // TIconEditView::PointToBit:  Converts the given mouse CPoint to an icon bit. 
  981. //----------------------------------------------------------------------------------------
  982. #pragma segment ARes
  983.  
  984. Boolean TIconEditView::PointToBit(const VPoint&    thePoint,
  985.                                          VPoint&        iconBit)
  986. {
  987.     VPoint    aPoint = thePoint;        // use a copy, since thePoint is a const
  988.     aPoint.h = aPoint.h - kBorder;            // Account for border in the edit view.
  989.     aPoint.v = aPoint.v - kBorder;
  990.  
  991.     // If the mouse is within the edit view's icon…
  992.     if ((aPoint.h >= 0) && (aPoint.h < kIconHBits * fMagnification) && 
  993.         (aPoint.v >= 0) && (aPoint.v < kIconVBits * fMagnification))
  994.     {
  995.         iconBit.h = aPoint.h / fMagnification;        // Convert from edit view CPoint
  996.         iconBit.v = aPoint.v / fMagnification;        // …to icon bit.
  997.         return TRUE;
  998.     }
  999.  
  1000.     return FALSE;                                // CPoint is not within the icon.
  1001. } // TIconEditView::PointToBit 
  1002.  
  1003.  
  1004. //----------------------------------------------------------------------------------------
  1005. // TIconEditView::GetMagnification: 
  1006. //----------------------------------------------------------------------------------------
  1007. #pragma segment ADoCommand
  1008.  
  1009. short TIconEditView::GetMagnification()
  1010. {
  1011.     return fMagnification;                        // SGet the new magnification.
  1012. } // TIconEditView::GetMagnification 
  1013.  
  1014.  
  1015. //----------------------------------------------------------------------------------------
  1016. // TIconEditView::SetMagnification: 
  1017. //----------------------------------------------------------------------------------------
  1018. #pragma segment ADoCommand
  1019.  
  1020. void TIconEditView::SetMagnification(short magnification)
  1021. {
  1022.     fMagnification = (short) Max(1, magnification);        // Set the new magnification.
  1023.     this->AdjustFrame();                        // Magnification affects the view's size.
  1024.     this->ForceRedraw();                        // Force the view to be entirely redrawn.
  1025. } // TIconEditView::SetMagnification 
  1026.  
  1027.  
  1028. //----------------------------------------------------------------------------------------
  1029. // TIconEditView::WriteToDeskScrap: 
  1030. //----------------------------------------------------------------------------------------
  1031. #pragma segment AClipboard
  1032.  
  1033. void TIconEditView::WriteToDeskScrap()
  1034. {
  1035.     fIconDocument->ReturnBitMap()->WriteToScrap();    // Write the icon data to the clipboard.
  1036.     Inherited::WriteToDeskScrap();                    // Write 'PICT' data to the desk scrap.
  1037. } // TIconEditView::WriteToDeskScrap 
  1038.  
  1039.  
  1040.  
  1041. //========================================================================================
  1042. // CLASS TDrawLineAppleEvent
  1043. //========================================================================================
  1044. #undef Inherited
  1045. #define Inherited TAppleEvent
  1046.  
  1047. #pragma segment ASelCommand
  1048. MA_DEFINE_CLASS_M1(TDrawPointsAppleEvent, Inherited);
  1049.  
  1050. //----------------------------------------------------------------------------------------
  1051. // TDrawLineAppleEvent    constructor
  1052. //----------------------------------------------------------------------------------------
  1053. #pragma segment ASelCommand
  1054.  
  1055. TDrawPointsAppleEvent::TDrawPointsAppleEvent()
  1056. {
  1057.     // nothing to do
  1058. } // TDrawPointsAppleEvent::TDrawPointsAppleEvent
  1059.  
  1060. //----------------------------------------------------------------------------------------
  1061. // TDrawPointsAppleEvent destructor
  1062. //----------------------------------------------------------------------------------------
  1063. #pragma segment MADestructorRes
  1064.  
  1065. TDrawPointsAppleEvent::~TDrawPointsAppleEvent()
  1066. {
  1067. }
  1068.  
  1069. //----------------------------------------------------------------------------------------
  1070. // TDrawPointsAppleEvent::IDrawPointsAppleEvent
  1071. //----------------------------------------------------------------------------------------
  1072. #pragma segment ASelCommand
  1073.  
  1074. void TDrawPointsAppleEvent::IDrawPointsAppleEvent(    MScriptableObject     *itsDirectObject,
  1075.                                                     TIconBitMap            *iconBitMapModel,
  1076.                                                     const Boolean        turnBitsOn)
  1077. {
  1078.     CTempDesc    directObjectDesc;
  1079.     VPoint        iconBit;
  1080.     CPoint        qdPoint;
  1081.     
  1082.     
  1083.     this->IAppleEvent(kAEIconEditClass, kAEDrawPoints, gServerAddress, kAENoReply+kAEDontExecute);
  1084.     itsDirectObject->MakeObjectSpecifier(directObjectDesc, formUniqueID);
  1085.     this->WriteParameter(keyDirectObject, directObjectDesc);
  1086.     
  1087.     TDynamicArray *pointArray = new TDynamicArray;    // create an array to store a list of points
  1088.     pointArray->IDynamicArray(1, sizeof(CPoint));
  1089.     
  1090.     // add the points set in the model to the dynamic array
  1091.     for (short vCoord = 0; vCoord < kIconVBits; ++vCoord)
  1092.     {
  1093.         iconBit.v = vCoord;
  1094.         for (short hCoord = 0; hCoord < kIconHBits; ++hCoord)
  1095.         {
  1096.             iconBit.h = hCoord;
  1097.             if (iconBitMapModel->GetBit(iconBit))
  1098.             {
  1099.                 qdPoint = iconBit.ToPoint();    // convert the iconBit to a CPoint
  1100.                 pointArray->InsertElementsBefore(pointArray->GetSize() + 1, &qdPoint, 1);
  1101.             }
  1102.         }
  1103.     }
  1104.     
  1105.     // write the point list to the apple event parameter list
  1106.     this->WritePtrList(keyPointList, typeQDPoint, pointArray);
  1107.                 
  1108.     pointArray = (TDynamicArray *)FreeIfObject(pointArray);
  1109.             
  1110.     // if turning the points off, add an optional parameter
  1111.     if (!turnBitsOn)
  1112.         this->WriteBoolean(keyErasePoints, TRUE);
  1113. } // TDrawPointsAppleEvent::IDrawPointsAppleEvent
  1114.     
  1115. //========================================================================================
  1116. // CLASS TInvertCommand
  1117. //========================================================================================
  1118. #undef Inherited
  1119. #define Inherited TCommand
  1120.  
  1121. #pragma segment ASelCommand
  1122. MA_DEFINE_CLASS_M1(TInvertCommand, Inherited);
  1123.  
  1124. //----------------------------------------------------------------------------------------
  1125. // TInvertCommand constructor 
  1126. //----------------------------------------------------------------------------------------
  1127. #pragma segment ASelCommand
  1128.  
  1129. TInvertCommand::TInvertCommand() 
  1130. {
  1131.     fIconDocument = NULL;
  1132. } // TInvertCommand::TInvertCommand 
  1133.  
  1134. //----------------------------------------------------------------------------------------
  1135. // TInvertCommand destructor
  1136. //----------------------------------------------------------------------------------------
  1137. #pragma segment MADestructorRes
  1138.  
  1139. TInvertCommand::~TInvertCommand()
  1140. {
  1141. }
  1142.  
  1143. //----------------------------------------------------------------------------------------
  1144. // TInvertCommand::IInvertCommand: 
  1145. //----------------------------------------------------------------------------------------
  1146. #pragma segment ASelCommand
  1147.  
  1148. void TInvertCommand::IInvertCommand(TIconDocument* itsIconDocument)
  1149. {
  1150.     this->ICommand(cInvert,                        // Initialize the inherited data…
  1151.                    itsIconDocument, 
  1152.                    kCanUndo, 
  1153.                    kCausesChange, 
  1154.                    itsIconDocument);            // …no view or scroller to track.
  1155.     fIconDocument = itsIconDocument;            // Save a reference to the icon document.
  1156. } // TInvertCommand::IInvertCommand 
  1157.  
  1158. //----------------------------------------------------------------------------------------
  1159. // TInvertCommand::MakeAppleEvent 
  1160. //----------------------------------------------------------------------------------------
  1161. TAppleEvent * TInvertCommand::MakeAppleEvent()
  1162. {
  1163.     CTempDesc    directObjectDesc;
  1164.     TAppleEvent * theInvertEvent = new TAppleEvent;
  1165.     
  1166.     theInvertEvent->IAppleEvent(kAEIconEditClass, kAEInvertID,gServerAddress, kAENoReply);
  1167.     fIconDocument->MakeObjectSpecifier(directObjectDesc, fIconDocument->GetSpecifierForm());    // create a descriptor
  1168.     theInvertEvent->WriteParameter(keyDirectObject, directObjectDesc);
  1169.     
  1170.     return theInvertEvent;
  1171. }
  1172.  
  1173. //----------------------------------------------------------------------------------------
  1174. // TInvertCommand::DoIt: 
  1175. //----------------------------------------------------------------------------------------
  1176. #pragma segment ADoCommand
  1177.  
  1178. void TInvertCommand::DoIt()
  1179. {
  1180.     fIconDocument->InvertIcon();                        // Invert the document's icon bitmap.
  1181. } // TInvertCommand::DoIt 
  1182.  
  1183.  
  1184. //----------------------------------------------------------------------------------------
  1185. // TInvertCommand::UndoIt: 
  1186. //----------------------------------------------------------------------------------------
  1187. #pragma segment ADoCommand
  1188.  
  1189. void TInvertCommand::UndoIt()
  1190. {
  1191.     fIconDocument->InvertIcon();                        // Uninvert the document's icon bitmap.
  1192. } // TInvertCommand::UndoIt 
  1193.  
  1194.  
  1195. //----------------------------------------------------------------------------------------
  1196. // TInvertCommand::RedoIt: 
  1197. //----------------------------------------------------------------------------------------
  1198. #pragma segment ADoCommand
  1199.  
  1200. void TInvertCommand::RedoIt()
  1201. {
  1202.     fIconDocument->InvertIcon();                        // Reinvert the document's icon bitmap.
  1203. } // TInvertCommand::RedoIt 
  1204.  
  1205. //========================================================================================
  1206. // CLASS TDrawPointsCommand
  1207. //========================================================================================
  1208. #undef Inherited
  1209. #define Inherited TCommand
  1210.  
  1211. #pragma segment ASelCommand
  1212. MA_DEFINE_CLASS_M1(TDrawPointsCommand, Inherited);
  1213.  
  1214. //----------------------------------------------------------------------------------------
  1215. // TSetPointsCommand constructor 
  1216. //----------------------------------------------------------------------------------------
  1217. #pragma segment ASelCommand
  1218.  
  1219. TDrawPointsCommand::TDrawPointsCommand()
  1220. {
  1221.     fSavedBitMap = NULL;
  1222.     fIconDocument = NULL;
  1223.     fErasePoints = FALSE;        // by default, turn bits on
  1224. } // TDrawPointsCommand::TDrawPointsCommand
  1225.  
  1226. //----------------------------------------------------------------------------------------
  1227. // TDrawPointsCommand::IDrawPointsCommand
  1228. //----------------------------------------------------------------------------------------
  1229. #pragma segment ASelCommand
  1230.  
  1231. void TDrawPointsCommand::IDrawPointsCommand(TIconDocument     *itsIconDocument,
  1232.                                             TAppleEvent        *theAppleEvent)
  1233. {
  1234.     fIconDocument = itsIconDocument;
  1235.     
  1236.     this->ICommand(cDrawPointsCommand,            // Initialize the inherited data…
  1237.                    itsIconDocument, 
  1238.                    kCanUndo, 
  1239.                    kCausesChange, 
  1240.                    itsIconDocument);
  1241.     
  1242.     // read the required pointlist parameter
  1243.     fPointList = new TDynamicArray;
  1244.     fPointList->IDynamicArray(1, sizeof(CPoint));
  1245.     
  1246.     theAppleEvent->ReadPtrList(keyPointList, typeQDPoint, fPointList);
  1247.             
  1248.     // read the optional erasePoints parameter
  1249.     if (theAppleEvent->HasParameter(keyErasePoints))
  1250.         fErasePoints = theAppleEvent->ReadBoolean(keyErasePoints);
  1251. } // TDrawLineCommand::IDrawLineCommand
  1252.  
  1253.  
  1254.  
  1255. //----------------------------------------------------------------------------------------
  1256. // TDrawPointsCommand::DoIt
  1257. //----------------------------------------------------------------------------------------
  1258. #pragma segment ADoCommand
  1259.  
  1260. void TDrawPointsCommand::DoIt()
  1261. {
  1262.     TIconBitMap    *targetBitMap = fIconDocument->ReturnBitMap();
  1263.     fSavedBitMap = targetBitMap->Copy();    // save the current bitmap
  1264.     
  1265.     // draw the points listed in the pointlist
  1266.     CArrayIterator    iter(fPointList);
  1267.     CPoint            aPoint;
  1268.     VPoint            iconBit;
  1269.     
  1270.     for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
  1271.     {
  1272.         fPointList->GetElementsAt(i, (Point *)aPoint, 1);
  1273.         iconBit.h = aPoint.h;
  1274.         iconBit.v = aPoint.v;
  1275.         targetBitMap->SetBit(iconBit, !fErasePoints);    // set the bit in the bitmap
  1276.     }
  1277.     
  1278.     fIconDocument->SetIcon(targetBitMap);                // this will cause a redraw
  1279.     // free the point list…it won't be used again
  1280.     fPointList = (TDynamicArray *)FreeIfObject(fPointList);
  1281.     
  1282. } // TDrawPointsCommand::DoIt
  1283.  
  1284. //----------------------------------------------------------------------------------------
  1285. // TDrawPointsCommand::UndoIt
  1286. //----------------------------------------------------------------------------------------
  1287. #pragma segment ADoCommand
  1288.  
  1289. void TDrawPointsCommand::UndoIt()
  1290. {
  1291.     TIconBitMap *tempBitMap = fIconDocument->ReturnBitMap()->Copy();    // save a local copy
  1292.     fIconDocument->SetIcon(fSavedBitMap);
  1293.     fSavedBitMap = tempBitMap;
  1294.     //fIconDocument->SetIcon(fOriginalBitMap);
  1295. } // TDrawPointsCommand::UndoIt
  1296.  
  1297.  
  1298. //----------------------------------------------------------------------------------------
  1299. // TDrawPointsCommand::RedoIt
  1300. //----------------------------------------------------------------------------------------
  1301. #pragma segment ADoCommand
  1302.  
  1303. void TDrawPointsCommand::RedoIt()
  1304. {
  1305.     TIconBitMap *tempBitMap = fIconDocument->ReturnBitMap()->Copy(); // save a local copy
  1306.     fIconDocument->SetIcon(fSavedBitMap);
  1307.     fSavedBitMap = tempBitMap;
  1308. } //  TDrawPointsCommand::RedoIt
  1309.  
  1310.  
  1311. //----------------------------------------------------------------------------------------
  1312. // TDrawLineCommand::Free
  1313. //----------------------------------------------------------------------------------------
  1314. #pragma segment ADoCommand
  1315.  
  1316. TDrawPointsCommand::~TDrawPointsCommand()
  1317. {
  1318.     if (fCommandDone)
  1319.         fSavedBitMap = (TIconBitMap *)FreeIfObject(fSavedBitMap);
  1320.         
  1321.     fPointList = (TDynamicArray *)FreeIfObject(fPointList);
  1322. } // TDrawPointsCommand::Free
  1323.  
  1324. //========================================================================================
  1325. // CLASS TSetColorCommand
  1326. //========================================================================================
  1327. #undef Inherited
  1328. #define Inherited TCommand
  1329.  
  1330. #pragma segment ASelCommand
  1331. MA_DEFINE_CLASS_M1(TSetColorCommand, Inherited);
  1332.  
  1333. //----------------------------------------------------------------------------------------
  1334. // TSetColorCommand constructor 
  1335. //----------------------------------------------------------------------------------------
  1336. #pragma segment ASelCommand
  1337.  
  1338. TSetColorCommand::TSetColorCommand()
  1339. {
  1340. } // TSetColorCommand::TSetColorCommand
  1341.  
  1342. //----------------------------------------------------------------------------------------
  1343. // TSetColorCommand destructor
  1344. //----------------------------------------------------------------------------------------
  1345. #pragma segment MADestructorRes
  1346.  
  1347. TSetColorCommand::~TSetColorCommand()
  1348. {
  1349. }
  1350.  
  1351. //----------------------------------------------------------------------------------------
  1352. // TSetColorCommand::ISetColorCommand
  1353. //----------------------------------------------------------------------------------------
  1354. #pragma segment ASelCommand
  1355.  
  1356. void TSetColorCommand::ISetColorCommand(TIconDocument     *itsDocument,
  1357.                                         CRGBColor&        newColor)
  1358. {
  1359.     this->ICommand(    cSetColor,            // Initialize the command…
  1360.                      itsDocument,        // Associate it with a document.
  1361.                     kCanUndo,
  1362.                     kCausesChange,
  1363.                     itsDocument);
  1364.     fNewColor = newColor;        
  1365.     fOldColor = itsDocument->GetIconColor();
  1366. } // TPickColorCommand::IPickColorCommand
  1367.  
  1368. //----------------------------------------------------------------------------------------
  1369. // TSetColorCommand::DoIt
  1370. //----------------------------------------------------------------------------------------
  1371. #pragma segment ADoCommand
  1372.  
  1373. void TSetColorCommand::DoIt()
  1374. {
  1375.     ((TIconDocument *)fContext)->SetIconColor(fNewColor);
  1376. } // TSetColorCommand::DoIt
  1377.  
  1378. //----------------------------------------------------------------------------------------
  1379. // TSetColorCommand::UndoIt
  1380. //----------------------------------------------------------------------------------------
  1381. #pragma segment ADoCommand
  1382.  
  1383. void TSetColorCommand::UndoIt()
  1384. {
  1385.     ((TIconDocument *)fContext)->SetIconColor(fOldColor);
  1386. } // TSetColorCommand::UndoIt
  1387.  
  1388. //----------------------------------------------------------------------------------------
  1389. // TSetColorCommand::RedoIt
  1390. //----------------------------------------------------------------------------------------
  1391. #pragma segment ADoCommand
  1392.  
  1393. void TSetColorCommand::RedoIt()
  1394. {
  1395.     ((TIconDocument *)fContext)->SetIconColor(fNewColor);
  1396. } // TSetColorCommand::RedoIt
  1397.     
  1398.     
  1399. //========================================================================================
  1400. // CLASS TIconDrawCommand
  1401. //========================================================================================
  1402. #undef Inherited
  1403. #define Inherited TTracker
  1404.  
  1405. #pragma segment ASelCommand
  1406. MA_DEFINE_CLASS_M1(TIconDrawCommand, Inherited);
  1407.  
  1408. //----------------------------------------------------------------------------------------
  1409. // TIconDrawCommand constructor 
  1410. //----------------------------------------------------------------------------------------
  1411. #pragma segment ASelCommand
  1412.  
  1413. TIconDrawCommand::TIconDrawCommand() 
  1414. {
  1415.     fIconDocument = NULL;
  1416.     fIconEditView = NULL;                    
  1417.     fIconBitMap = NULL;                                    
  1418.     fOriginalIcon = NULL;
  1419.     fTurnBitsOn = TRUE;
  1420.     fIconBitMapModel = NULL;
  1421. } // TIconDrawCommand::TIconDrawCommand 
  1422.  
  1423. //----------------------------------------------------------------------------------------
  1424. // TIconDrawCommand::IIconDrawCommand: 
  1425. //----------------------------------------------------------------------------------------
  1426. #pragma segment ASelCommand
  1427.  
  1428. void TIconDrawCommand::IIconDrawCommand(TIconEditView*    itsIconEditView,
  1429.                                                TIconDocument*    itsIconDocument,
  1430.                                                VPoint&            theMouse)
  1431. {
  1432.     this->ITracker(    cDrawCommand,                        // Initialize the command… 
  1433.                     itsIconDocument,                    // Its context
  1434.                      kCanUndo, 
  1435.                      kCausesChange, 
  1436.                      itsIconDocument,                    // Associate it with a notifying object.
  1437.                     itsIconEditView,                    // Associate it with a view.
  1438.                      itsIconEditView->GetScroller(TRUE),    // Associate it with a scroller
  1439.                      theMouse);                            
  1440.     fConstrainsMouse = TRUE;                            // Want TrackConstrain called.
  1441.  
  1442.     fIconEditView = itsIconEditView;                    // Set some convenience fields…
  1443.     fIconDocument = itsIconDocument;
  1444.     fIconBitMap = fIconDocument->ReturnBitMap();        // Get reference to icon being drawn.
  1445.     fIconBitMapModel = new TIconBitMap;                    // create a model for the bits being set
  1446.     fIconBitMapModel->IIconBitMap();
  1447.     fIconBitMapModel->Clear();
  1448.     
  1449.     fColor = fIconDocument->GetIconColor();                    // store the drawing color
  1450. } // TIconDrawCommand::IIconDrawCommand 
  1451.  
  1452.  
  1453. //----------------------------------------------------------------------------------------
  1454. // TIconDrawCommand::Free: 
  1455. //----------------------------------------------------------------------------------------
  1456. #pragma segment ADoCommand
  1457.  
  1458. TIconDrawCommand::~TIconDrawCommand()
  1459. {
  1460.     if (fCommandDone)                                                // If the command is done…
  1461.         fOriginalIcon = (TIconBitMap*)FreeIfObject(fOriginalIcon);    // …dispose of original icon.
  1462.     else                                                            // Else if command is undone…
  1463.         fIconBitMap = (TIconBitMap*)FreeIfObject(fIconBitMap);        // …dispose of icon that was drawn
  1464.         
  1465.     fIconBitMapModel = (TIconBitMap*)FreeIfObject(fIconBitMapModel);    // free the bitmap model
  1466. } // TIconDrawCommand::Free 
  1467.  
  1468. //----------------------------------------------------------------------------------------
  1469. // TIconDrawCommand::TrackConstrain: 
  1470. //----------------------------------------------------------------------------------------
  1471. #pragma segment ADoCommand
  1472.  
  1473. void TIconDrawCommand::TrackConstrain(TrackPhase        /*aTrackPhase*/,
  1474.                                              const VPoint&    /*anchorPoint*/ ,
  1475.                                              const VPoint&    /*previousPoint*/ ,
  1476.                                              VPoint&        nextPoint,
  1477.                                              Boolean        /*mouseDidMove*/)
  1478. {
  1479.     // This is on several lines so that Max can be "inlined"
  1480.     VCoordinate h = Min(nextPoint.h, fIconEditView->fSize.h - kBorder - 1);
  1481.     VCoordinate v = Min(nextPoint.v, fIconEditView->fSize.v - kBorder - 1);    
  1482.     h = Max(h, (long) kBorder);
  1483.     v = Max(v, (long) kBorder);
  1484.     nextPoint = VPoint(h, v);
  1485. } // TIconDrawCommand::TrackConstrain 
  1486.  
  1487.  
  1488. //----------------------------------------------------------------------------------------
  1489. // TIconDrawCommand::TrackFeedback: 
  1490. //----------------------------------------------------------------------------------------
  1491. #pragma segment ADoCommand
  1492.  
  1493. void TIconDrawCommand::TrackFeedback(TrackPhase        /*aTrackPhase*/,
  1494.                                             const VPoint&    /*anchorPoint*/,
  1495.                                             const VPoint&    /*previousPoint*/,
  1496.                                             const VPoint&    /*nextPoint*/,
  1497.                                             Boolean            /*mouseDidMove*/,
  1498.                                             Boolean            /*turnItOn*/)
  1499. {
  1500.     // Overridden to avoid standard feedback.
  1501. } // TIconDrawCommand::TrackFeedback 
  1502.  
  1503.  
  1504. //----------------------------------------------------------------------------------------
  1505. // TIconDrawCommand::TrackMouse: 
  1506. //----------------------------------------------------------------------------------------
  1507. #pragma segment ADoCommand
  1508.  
  1509. TTracker* TIconDrawCommand::TrackMouse(TrackPhase    aTrackPhase,
  1510.                                               VPoint&        /*anchorPoint*/,
  1511.                                               VPoint&        previousPoint,
  1512.                                               VPoint&        nextPoint,
  1513.                                               Boolean        mouseDidMove)
  1514. {
  1515.     VPoint    fromBit;
  1516.     VPoint    toBit;
  1517.     VPoint    iconBit;
  1518.     short    lineLength;
  1519.     float    deltaH;
  1520.     float    deltaV;
  1521.     float    h;
  1522.     float    v;    
  1523.     
  1524.     // Convert nextPoint and previousPoint to bit locations in the icon…
  1525.     fIconEditView->PointToBit(fIconEditView->ViewToQDPt(nextPoint), toBit);
  1526.     fIconEditView->PointToBit(fIconEditView->ViewToQDPt(previousPoint), fromBit);
  1527.     
  1528.     if (aTrackPhase == trackBegin)                    // first time through
  1529.     {
  1530.         fTurnBitsOn =!fIconBitMap->GetBit(toBit);    // Turn bits on or off?
  1531.         fOriginalIcon = fIconBitMap->Copy();        // Make a copy of the original for undo
  1532.     }
  1533.     
  1534.     if (mouseDidMove)                                    // If mouse moved since last time…
  1535.     {
  1536.         // The following sets bits in the icon from the bit at previousPoint to the bit
  1537.         // at nextPoint.  It is based on a simple line-drawing algorithm.
  1538.  
  1539.         lineLength = (short) Max(labs(toBit.h - fromBit.h), labs(toBit.v - fromBit.v));
  1540.         lineLength = (short) Max(1,lineLength);    // This is on two lines so that Max can be "inlined"
  1541.         
  1542.         deltaH = (toBit.h - fromBit.h) / lineLength;
  1543.         deltaV = (toBit.v - fromBit.v) / lineLength;
  1544.  
  1545.         h = fromBit.h + 0.5;
  1546.         v = fromBit.v + 0.5;
  1547.         
  1548.         for (short i = 0; i < lineLength; i++)
  1549.         {
  1550.             iconBit.h = h;
  1551.             iconBit.v = v;
  1552.             
  1553.             fIconBitMap->SetBit(iconBit, fTurnBitsOn);// Set the bit in the icon
  1554.             fIconEditView->DrawBit(iconBit, fTurnBitsOn, fColor);// …and draw it in the edit view.
  1555.             
  1556.             // set the bit in the bitMapView model.  this model will be used to generate
  1557.             // a setpoints apple event for recordability.
  1558.             fIconBitMapModel->SetBit(iconBit, TRUE);
  1559.  
  1560.             h = h + deltaH;
  1561.             v = v + deltaV;
  1562.         }
  1563.     }
  1564.     
  1565.     if (aTrackPhase == trackEnd)                    // when tracking is done, post a
  1566.     {                                                // recordable apple event            
  1567.         TDrawPointsAppleEvent *aDrawEvent = new TDrawPointsAppleEvent;
  1568.         aDrawEvent->IDrawPointsAppleEvent(fIconDocument, fIconBitMapModel, fTurnBitsOn);
  1569.         aDrawEvent->Send();
  1570.     }
  1571.     return this;                                // Return same command object.
  1572. } // TIconDrawCommand::TrackMouse 
  1573.  
  1574. //----------------------------------------------------------------------------------------
  1575. // TIconDrawCommand::DoIt:
  1576. //----------------------------------------------------------------------------------------
  1577. #pragma segment ADoCommand
  1578.  
  1579. void TIconDrawCommand::DoIt()
  1580. {
  1581.     VRect    editViewExtent;
  1582.     
  1583.     fIconDocument->SetIcon(fIconBitMap);    // Set the document's bit map
  1584.     
  1585.     // SetIcon invalidates all views of the document, including the one we just drew in.
  1586.     // To avoid flashing, validate the edit view here so it doesn't get redrawn.
  1587.     
  1588.     fIconEditView->GetExtent(editViewExtent);
  1589.     fIconEditView->ValidateVRect(editViewExtent);
  1590. } // TIconDrawCommand::DoIt
  1591.  
  1592. //----------------------------------------------------------------------------------------
  1593. // TIconDrawCommand::UndoIt: 
  1594. //----------------------------------------------------------------------------------------
  1595. #pragma segment ADoCommand
  1596.  
  1597. void TIconDrawCommand::UndoIt()
  1598. {    
  1599.     // Save a local copy of the current bit map, and reset it after the call to
  1600.     // SetIcon.  This is necessary since SetIcon frees the current bitmap    
  1601.     TIconBitMap* tempBitMap = fIconBitMap->Copy();
  1602.     fIconDocument->SetIcon(fOriginalIcon);                // Restore icon to original state.
  1603.     fIconBitMap = tempBitMap;
  1604. } // TIconDrawCommand::UndoIt 
  1605.  
  1606. //----------------------------------------------------------------------------------------
  1607. // TIconDrawCommand::RedoIt: 
  1608. //----------------------------------------------------------------------------------------
  1609. #pragma segment ADoCommand
  1610.  
  1611. void TIconDrawCommand::RedoIt()
  1612. {
  1613.     // save a local copy of the current bit map, and reset it after the call to
  1614.     // SetIcon.  This is necessary since SetIcon frees the current bitmap
  1615.     TIconBitMap* tempBitMap = fOriginalIcon->Copy();
  1616.     fIconDocument->SetIcon(fIconBitMap);        // Set the icon back to the new one.
  1617.     fOriginalIcon = tempBitMap;
  1618. } // TIconDrawCommand::RedoIt 
  1619.  
  1620.  
  1621. //========================================================================================
  1622. // CLASS TIconEditCommand
  1623. //========================================================================================
  1624. #undef Inherited
  1625. #define Inherited TCommand
  1626.  
  1627. #pragma segment ASelCommand
  1628. MA_DEFINE_CLASS_M1(TIconEditCommand, Inherited);
  1629.  
  1630. //----------------------------------------------------------------------------------------
  1631. // TIconEditCommand::Initialize: 
  1632. //----------------------------------------------------------------------------------------
  1633. #pragma segment ASelCommand
  1634.  
  1635. TIconEditCommand::TIconEditCommand() 
  1636. {
  1637.     fIconDocument = NULL;
  1638.     fIconEditView = NULL;                    
  1639.     fSavedBitMap = NULL;                                    
  1640. } // TIconEditCommand::TIconEditCommand 
  1641.  
  1642. //----------------------------------------------------------------------------------------
  1643. // TIconEditCommand::IIconEditCommand: 
  1644. //----------------------------------------------------------------------------------------
  1645. #pragma segment ASelCommand
  1646.  
  1647. void TIconEditCommand::IIconEditCommand (CommandNumber    itsCommandNumber, 
  1648.                                                 TIconEditView*    itsIconEditView,
  1649.                                                   TIconDocument*    itsIconDocument)
  1650.  
  1651. {
  1652.     this->ICommand(    itsCommandNumber,                    // Initialize the command…
  1653.                      itsIconDocument,                    // Associate it with a document.
  1654.                     kCanUndo,
  1655.                     kCausesChange,
  1656.                     itsIconDocument);
  1657.  
  1658.     fChangesClipboard = (itsCommandNumber != cClear);    // Cut or Copy changes the clipboard.            
  1659.     fCausesChange = (itsCommandNumber != cCopy);        // Cut or Clear changes the documnt.
  1660.  
  1661.     fIconEditView = itsIconEditView;                    // Remember reference to the view.
  1662.     fIconDocument = itsIconDocument;                    // Remember reference to the document.
  1663.     fSavedBitMap = fIconDocument->ReturnBitMap()->Copy();// Save a copy of the current bitmap.
  1664. } // TIconEditCommand::IIconEditCommand 
  1665.  
  1666.  
  1667. //----------------------------------------------------------------------------------------
  1668. // TIconEditCommand::Free: 
  1669. //----------------------------------------------------------------------------------------
  1670. #pragma segment ADoCommand
  1671.  
  1672. TIconEditCommand::~TIconEditCommand()
  1673. {
  1674.     fSavedBitMap = (TIconBitMap*)FreeIfObject(fSavedBitMap);    // Free the saved bitMap
  1675. } // TIconEditCommand::Free 
  1676.  
  1677.  
  1678. //----------------------------------------------------------------------------------------
  1679. // TIconEditCommand::DoIt: 
  1680. //----------------------------------------------------------------------------------------
  1681. #pragma segment ADoCommand
  1682.  
  1683. void TIconEditCommand::DoIt()
  1684. {
  1685.     if (fIdentifier != cClear)                    // If the command is Cut or Copy…
  1686.     {
  1687.         TIconDocument*    clipDocument;
  1688.         TIconEditView* clipView;
  1689.  
  1690.         // …copy the data to the clipboard.
  1691.  
  1692.         // Need a document to represent the data to be put on the clipboard.
  1693.         // If IIconDocument fails, it should free itself
  1694.         clipDocument = new TIconDocument;
  1695.         clipDocument->IIconDocument(NULL);
  1696.     
  1697.         // If IIconEditView fails it should free itself, but we need to free the document
  1698.         FailInfo fi;
  1699.         Try(fi)
  1700.         {
  1701.             clipView = new TIconEditView;
  1702.             clipView->IIconEditView(clipDocument, NULL, gZeroVPt, fIconEditView->GetMagnification());
  1703.             
  1704.             fSavedBitMap->CopyDataTo(clipDocument->ReturnBitMap());// Copy data to document icon.
  1705.  
  1706.             fi.Success();                        // Don't need failure handler anymore.
  1707.         }
  1708.         else // Recover
  1709.         {
  1710.             clipDocument = (TIconDocument*)FreeIfObject(clipDocument);// Free clipDocument if non-NULL.
  1711.             fi.ReSignal();
  1712.         }
  1713.  
  1714.         // Tell the app we have a new clipboard.
  1715.         this->ClaimClipboard(clipView);
  1716.     }
  1717.  
  1718.     if (fIdentifier != cCopy)                    // Cut or Clear commands clear the icon.
  1719.         fIconDocument->ClearIcon();                // Clear the icon.
  1720.  
  1721. } // TIconEditCommand::DoIt 
  1722.  
  1723.  
  1724. //----------------------------------------------------------------------------------------
  1725. // TIconEditCommand::RedoIt: 
  1726. //----------------------------------------------------------------------------------------
  1727. #pragma segment ADoCommand
  1728.  
  1729. void TIconEditCommand::RedoIt()
  1730. {
  1731.     if (fIdentifier != cCopy)                    // Cut or Clear commands clear the icon.
  1732.         fIconDocument->ClearIcon();
  1733. } // TIconEditCommand::RedoIt 
  1734.  
  1735.  
  1736. //----------------------------------------------------------------------------------------
  1737. // TIconEditCommand::UndoIt: 
  1738. //----------------------------------------------------------------------------------------
  1739. #pragma segment ADoCommand
  1740.  
  1741. void TIconEditCommand::UndoIt()
  1742. {                                            // If the command was Cut or Clear, the new
  1743.     if (fIdentifier != cCopy)                // …icon was cleared, so restore it.
  1744.     {    
  1745.         fSavedBitMap->CopyDataTo(fIconDocument->ReturnBitMap());
  1746.         fIconDocument->RedrawViews();        // Make sure all views get redrawn.
  1747.     }
  1748. } // TIconEditCommand::UndoIt 
  1749.  
  1750. //========================================================================================
  1751. // CLASS TIconPasteCommand
  1752. //========================================================================================
  1753. #undef Inherited
  1754. #define Inherited TCommand
  1755.  
  1756. #pragma segment ASelCommand
  1757. MA_DEFINE_CLASS_M1(TIconPasteCommand, Inherited);
  1758.  
  1759. //----------------------------------------------------------------------------------------
  1760. // TIconPasteCommand constructor 
  1761. //----------------------------------------------------------------------------------------
  1762. #pragma segment ASelCommand
  1763.  
  1764. TIconPasteCommand::TIconPasteCommand()    
  1765. {
  1766.     fIconDocument = NULL;
  1767.     fIconEditView = NULL;                    
  1768.     fSavedIcon = NULL;
  1769. } // TIconPasteCommand::TIconPasteCommand 
  1770.  
  1771. //----------------------------------------------------------------------------------------
  1772. // TIconPasteCommand::IIconPasteCommand: 
  1773. //----------------------------------------------------------------------------------------
  1774. #pragma segment ASelCommand
  1775.  
  1776. void TIconPasteCommand::IIconPasteCommand(    TIconEditView* itsIconEditView,
  1777.                                             TIconDocument* itsIconDocument)
  1778. {
  1779.     this->ICommand(cPaste,                            // Initialize the command…
  1780.                    itsIconDocument,                    // Associate it with a document.
  1781.                    kCanUndo, 
  1782.                    kCausesChange, 
  1783.                    itsIconDocument);
  1784.  
  1785.     fIconEditView = itsIconEditView;                        // Remember reference to the view.
  1786.     fIconDocument = itsIconDocument;                        // Remember reference to the document.
  1787.     
  1788.     TIconDocument*    clipDoc = (TIconDocument*)gClipboardMgr->fClipView->fDocument;
  1789.     fSavedIcon = clipDoc->ReturnBitMap()->Copy();
  1790. } // TIconPasteCommand::IIconPasteCommand 
  1791.  
  1792. //----------------------------------------------------------------------------------------
  1793. // TIconPasteCommand::Free: 
  1794. //----------------------------------------------------------------------------------------
  1795. #pragma segment ADoCommand
  1796.  
  1797. TIconPasteCommand::~TIconPasteCommand()
  1798. {
  1799.     fSavedIcon = (TIconBitMap*)FreeIfObject(fSavedIcon);
  1800. } // TIconPasteCommand::Free 
  1801.  
  1802. //----------------------------------------------------------------------------------------
  1803. // TIconPasteCommand::DoIt: 
  1804. //----------------------------------------------------------------------------------------
  1805. #pragma segment ADoCommand
  1806.  
  1807. void TIconPasteCommand::DoIt()
  1808. {
  1809.     TIconBitMap* tempIcon = fIconDocument->ReturnBitMap()->Copy();
  1810.     fIconDocument->SetIcon(fSavedIcon);
  1811.     fSavedIcon = tempIcon;
  1812. } // TIconPasteCommand::DoIt 
  1813.  
  1814. //----------------------------------------------------------------------------------------
  1815. // TIconPasteCommand::UndoIt: 
  1816. //----------------------------------------------------------------------------------------
  1817. #pragma segment ADoCommand
  1818.  
  1819. void TIconPasteCommand::UndoIt()
  1820. {
  1821.     this->DoIt();
  1822. } // TIconPasteCommand::UndoIt 
  1823.  
  1824. //========================================================================================
  1825. // CLASS TIconBitMap
  1826. //========================================================================================
  1827. #undef Inherited
  1828. #define Inherited TObject
  1829.  
  1830. #pragma segment AOpen
  1831. MA_DEFINE_CLASS_M1(TIconBitMap, Inherited);
  1832.  
  1833. //----------------------------------------------------------------------------------------
  1834. // TIconBitMap constructor 
  1835. //----------------------------------------------------------------------------------------
  1836. #pragma segment AOpen
  1837.  
  1838. TIconBitMap::TIconBitMap() 
  1839. {
  1840.     fDataHandle = NULL;
  1841. } // TIconBitMap::TIconBitMap 
  1842.  
  1843. //----------------------------------------------------------------------------------------
  1844. // TIconBitMap::IIconBitMap: 
  1845. //----------------------------------------------------------------------------------------
  1846. #pragma segment ARes
  1847.  
  1848. void TIconBitMap::IIconBitMap()
  1849. {
  1850.     this->IObject();
  1851.     
  1852.     FailInfo fi;
  1853.     Try(fi)
  1854.     {
  1855.         fDataHandle = NewPermHandle(kIconSizeInBytes);    // Allocate a handle for the
  1856.                                                         // bitmap.
  1857.         fi.Success();
  1858.     }
  1859.     else
  1860.     {
  1861.         this->Free();
  1862.         fi.ReSignal();
  1863.     }
  1864. } // TIconBitMap::IIconBitMap 
  1865.  
  1866.  
  1867. //----------------------------------------------------------------------------------------
  1868. // TIconBitMap::Free: 
  1869. //----------------------------------------------------------------------------------------
  1870. #pragma segment ARes
  1871.  
  1872. TIconBitMap::~TIconBitMap()
  1873. {
  1874.     fDataHandle = DisposeIfHandle(fDataHandle);            // dispose of icon data.
  1875. } // TIconBitMap::Free 
  1876.  
  1877. //----------------------------------------------------------------------------------------
  1878. // TIconBitMap::SetIconBitMap: 
  1879. //----------------------------------------------------------------------------------------
  1880. #pragma segment ARes
  1881.  
  1882. void TIconBitMap::SetIconBitMap(Handle theBitMap)
  1883. {
  1884.     MABlockMove(*theBitMap, 
  1885.                 *fDataHandle,                    // …then copy it into the document's
  1886.                 kIconSizeInBytes);                // …icon bitmap.
  1887. } // TIconBitMap::SetIconBitMap 
  1888.  
  1889.  
  1890. //----------------------------------------------------------------------------------------
  1891. // TIconBitMap::ReadFrom: 
  1892. //----------------------------------------------------------------------------------------
  1893. #pragma segment AReadFile
  1894.  
  1895. void TIconBitMap::ReadFrom(TStream* aStream)
  1896. {
  1897.     long numberOfBytes;
  1898.  
  1899.     numberOfBytes = kIconSizeInBytes;
  1900.     aStream->ReadBytes(*fDataHandle, numberOfBytes);
  1901. } // TIconBitMap::ReadFrom 
  1902.  
  1903.  
  1904. //----------------------------------------------------------------------------------------
  1905. // TIconBitMap::WriteTo: 
  1906. //----------------------------------------------------------------------------------------
  1907. #pragma segment AWriteFile
  1908.  
  1909. void TIconBitMap::WriteTo(TStream* aStream)
  1910. {
  1911.     long numberOfBytes;
  1912.  
  1913.     numberOfBytes = kIconSizeInBytes;
  1914.     aStream->WriteBytes(*fDataHandle, numberOfBytes);
  1915. } // TIconBitMap::WriteTo 
  1916.  
  1917.  
  1918. //----------------------------------------------------------------------------------------
  1919. // TIconBitMap::LoadFromScrap: 
  1920. //----------------------------------------------------------------------------------------
  1921. #pragma segment AClipboard
  1922.  
  1923. void TIconBitMap::LoadFromScrap()
  1924. {
  1925.     long offset;
  1926.     long err;
  1927.  
  1928.     err = GetScrap(fDataHandle, kIconClipType, &offset);    // Load the icon's data from the
  1929.     if (err < 0)
  1930.         FailOSErr((short) err);                        // the clip board and handle errors.
  1931. } // TIconBitMap::LoadFromScrap 
  1932.  
  1933.  
  1934. //----------------------------------------------------------------------------------------
  1935. // TIconBitMap::WriteToScrap: 
  1936. //----------------------------------------------------------------------------------------
  1937. #pragma segment AClipboard
  1938.  
  1939. void TIconBitMap::WriteToScrap()
  1940. {
  1941.     FailOSErr(gClipboardMgr->PutDeskScrapData(kIconClipType,    // Write the handle to the desk scrap, //new
  1942.                                fDataHandle));                    // …failing if an error occurs.
  1943. } // TIconBitMap::WriteToScrap 
  1944.             
  1945. //----------------------------------------------------------------------------------------
  1946. // TIconBitMap::Copy: 
  1947. //----------------------------------------------------------------------------------------
  1948. #pragma segment ARes
  1949.  
  1950. TIconBitMap* TIconBitMap::Copy()
  1951. {
  1952.     TIconBitMap* copyOfIcon;
  1953.  
  1954.     copyOfIcon = new TIconBitMap;                // Create a TIconBitMap object.
  1955.     copyOfIcon->IIconBitMap();                    // Initialize it.
  1956.     copyOfIcon->SetIconBitMap(fDataHandle);        // Copy the data.
  1957.     return copyOfIcon;                            // Return a reference to the new handle.
  1958. } // TIconBitMap::Copy 
  1959.  
  1960.  
  1961. //----------------------------------------------------------------------------------------
  1962. // TIconBitMap::CopyDataTo: 
  1963. //----------------------------------------------------------------------------------------
  1964. #pragma segment ARes
  1965.  
  1966. void TIconBitMap::CopyDataTo(TIconBitMap* anIcon)
  1967. {
  1968.     anIcon->SetIconBitMap(fDataHandle);            // Copy data to the new icon.
  1969. } // TIconBitMap::CopyDataTo 
  1970.  
  1971.  
  1972. //----------------------------------------------------------------------------------------
  1973. // TIconBitMap::Clear: 
  1974. //----------------------------------------------------------------------------------------
  1975. #pragma segment ARes
  1976.  
  1977. void TIconBitMap::Clear()
  1978. {
  1979.     long* longPtr;
  1980.  
  1981.     longPtr = (long*) * fDataHandle;
  1982.     for (short i = 0; i < kIconSizeInLongs; i++)
  1983.         *longPtr++ = 0;
  1984. } // TIconBitMap::Clear 
  1985.  
  1986.  
  1987. //----------------------------------------------------------------------------------------
  1988. // TIconBitMap::Invert: 
  1989. //----------------------------------------------------------------------------------------
  1990. #pragma segment ARes
  1991.  
  1992. void TIconBitMap::Invert()
  1993. {
  1994.     long* longPtr;
  1995.  
  1996.     longPtr = (long*) * fDataHandle;
  1997.     for (short i = 0; i < kIconSizeInLongs; i++)
  1998.     {
  1999.         *longPtr =~*longPtr;
  2000.         longPtr++;
  2001.     }
  2002. } // TIconBitMap::Invert 
  2003.  
  2004.  
  2005. //----------------------------------------------------------------------------------------
  2006. // TIconBitMap::Draw: 
  2007. //----------------------------------------------------------------------------------------
  2008. #pragma segment ARes
  2009.  
  2010. void TIconBitMap::Draw(    const CRect&         area,
  2011.                         const CRGBColor&     drawingColor)
  2012. {
  2013.     CRGBColor saveColor;
  2014.     
  2015.     GetIfColor(saveColor);
  2016.     SetIfColor(drawingColor);
  2017.     PenNormal();
  2018.     PlotIcon(area, fDataHandle);
  2019.     SetIfColor(saveColor);
  2020. } // TIconBitMap::Draw 
  2021.  
  2022.  
  2023. //----------------------------------------------------------------------------------------
  2024. // TIconBitMap::GetBit: 
  2025. //----------------------------------------------------------------------------------------
  2026. #pragma segment ARes
  2027.  
  2028. Boolean TIconBitMap::GetBit(VPoint iconBit)
  2029. //  Returns the state of the given bit in the icon being drawn. 
  2030. {
  2031.     short byte;
  2032.     short bitInByte;
  2033.  
  2034.     this->IconBitToByteBit(iconBit, byte, bitInByte);
  2035.  
  2036.     return (*(*fDataHandle + byte) >> bitInByte) & 1;
  2037. } // TIconBitMap::GetBit 
  2038.  
  2039.  
  2040. //----------------------------------------------------------------------------------------
  2041. // TIconBitMap::SetBit:  Set the state of the given bit in the icon being drawn. 
  2042. //----------------------------------------------------------------------------------------
  2043. #pragma segment ARes
  2044.  
  2045. void TIconBitMap::SetBit(VPoint    iconBit,
  2046.                                 Boolean    turnBitOn)
  2047. {
  2048.     short    byte;
  2049.     short    bitInByte;
  2050.     char    theMask;
  2051.  
  2052.     this->IconBitToByteBit(iconBit, byte, bitInByte);
  2053.  
  2054.     theMask = (1 << bitInByte);
  2055.  
  2056.     if (turnBitOn)
  2057.         *(*fDataHandle + byte) |= theMask;
  2058.     else
  2059.         *(*fDataHandle + byte) &= ~theMask;        // AND the complement of the mask.
  2060. } // TIconBitMap::SetBit 
  2061.  
  2062.  
  2063. //----------------------------------------------------------------------------------------
  2064. // TIconBitMap::IconBitToByteBit: This converts the given icon bit to a byte and bit in an
  2065. // array of long words.
  2066. //----------------------------------------------------------------------------------------
  2067. #pragma segment ARes
  2068.  
  2069. void TIconBitMap::IconBitToByteBit(VPoint iconBit, short& byte, short& bit)
  2070. {
  2071.     long bitNumber;
  2072.  
  2073.     bitNumber = iconBit.v * kIconVBits + iconBit.h;
  2074.     byte = (short) bitNumber / 8;
  2075.     bit = (short) (7 - (bitNumber % 8));
  2076. } // TIconBitMap::IconBitToByteBit 
  2077.  
  2078.  
  2079. //========================================================================================
  2080. // CLASS TZoomOutCommand
  2081. //========================================================================================
  2082. #undef Inherited
  2083. #define Inherited TCommand
  2084.  
  2085. #pragma segment ASelCommand
  2086. MA_DEFINE_CLASS_M1(TZoomOutCommand, Inherited);
  2087.  
  2088. //----------------------------------------------------------------------------------------
  2089. // TZoomOutCommand constructor 
  2090. //----------------------------------------------------------------------------------------
  2091. #pragma segment ASelCommand
  2092.  
  2093. TZoomOutCommand::TZoomOutCommand() 
  2094. {
  2095.     fIconDocument = NULL;
  2096. } // TZoomOutCommand::TZoomOutCommand()  
  2097.  
  2098. //----------------------------------------------------------------------------------------
  2099. // TZoomOutCommand destructor
  2100. //----------------------------------------------------------------------------------------
  2101. #pragma segment MADestructorRes
  2102.  
  2103. TZoomOutCommand::~TZoomOutCommand()
  2104. {
  2105. }
  2106.  
  2107. //----------------------------------------------------------------------------------------
  2108. // TZoomOutCommand::IZoomOutCommand
  2109. //----------------------------------------------------------------------------------------
  2110. #pragma segment ASelCommand
  2111.  
  2112. void  TZoomOutCommand::IZoomOutCommand(TIconDocument* itsIconDocument)
  2113. {
  2114.       fIconDocument = itsIconDocument;            // Save a reference to the icon document.
  2115.  
  2116.     this->ICommand(cZoomOut,                        // Initialize the inherited data…
  2117.                    itsIconDocument, 
  2118.                    kCanUndo, 
  2119.                    kCausesChange, 
  2120.                    itsIconDocument);            // …no view or scroller to track.
  2121. } //TZoomOutCommand::IZoomOutCommand 
  2122.  
  2123. //----------------------------------------------------------------------------------------
  2124. // TZoomOutCommand::MakeAppleEvent() 
  2125. //----------------------------------------------------------------------------------------
  2126. TAppleEvent * TZoomOutCommand::MakeAppleEvent()
  2127. {
  2128.     CTempDesc    directObjectDesc;
  2129.     TAppleEvent * theZoomOutAppleEvent = new TAppleEvent;
  2130.     
  2131.     theZoomOutAppleEvent->IAppleEvent(kAEIconEditClass, kAEZoomOutID,gServerAddress, kAENoReply);
  2132.     fIconDocument->MakeObjectSpecifier(directObjectDesc, fIconDocument->GetSpecifierForm());
  2133.     theZoomOutAppleEvent->WriteParameter(keyDirectObject, directObjectDesc);
  2134.     
  2135.     return theZoomOutAppleEvent;
  2136. } //TZoomOutCommand::MakeAppleEvent
  2137.  
  2138. //----------------------------------------------------------------------------------------
  2139. // TZoomOutCommand::DoIt: 
  2140. //----------------------------------------------------------------------------------------
  2141. #pragma segment ADoCommand
  2142.  
  2143. void TZoomOutCommand::DoIt()
  2144. {   
  2145.     short    currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2146.     fIconDocument->GetIconView()->SetMagnification(currentMagnification - 2);
  2147. } // TZoomOutCommand::DoIt 
  2148.  
  2149.  
  2150. //----------------------------------------------------------------------------------------
  2151. // TZoomOutCommand::UndoIt: 
  2152. //----------------------------------------------------------------------------------------
  2153. #pragma segment ADoCommand
  2154.  
  2155. void TZoomOutCommand::UndoIt()
  2156. {
  2157.     short    currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2158.     fIconDocument->GetIconView()->SetMagnification(currentMagnification + 2);
  2159. } // TZoomOutCommand::UndoIt 
  2160.  
  2161.  
  2162. //----------------------------------------------------------------------------------------
  2163. // TZoomOutCommand::RedoIt: 
  2164. //----------------------------------------------------------------------------------------
  2165. #pragma segment ADoCommand
  2166.  
  2167. void TZoomOutCommand::RedoIt()
  2168. {
  2169.      short    currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2170.     fIconDocument->GetIconView()->SetMagnification(currentMagnification - 2);
  2171. }  // TZoomOutCommand::RedoIt 
  2172.  
  2173. //========================================================================================
  2174. // CLASS TZoomInCommand
  2175. //========================================================================================
  2176. #undef Inherited
  2177. #define Inherited TCommand
  2178.  
  2179. #pragma segment ASelCommand
  2180. MA_DEFINE_CLASS_M1(TZoomInCommand, Inherited);
  2181.  
  2182. //----------------------------------------------------------------------------------------
  2183. // TZoomInCommand constructor 
  2184. //----------------------------------------------------------------------------------------
  2185. #pragma segment ASelCommand
  2186.  
  2187. TZoomInCommand::TZoomInCommand() 
  2188. {
  2189.     fIconDocument = NULL;
  2190. } // TZoomInCommand::TZoomInCommand()  
  2191.  
  2192. //----------------------------------------------------------------------------------------
  2193. // TZoomInCommand destructor
  2194. //----------------------------------------------------------------------------------------
  2195. #pragma segment MADestructorRes
  2196.  
  2197. TZoomInCommand::~TZoomInCommand()
  2198. {
  2199. }
  2200.  
  2201. //----------------------------------------------------------------------------------------
  2202. // TZoomInCommand::IZoomInCommand
  2203. //----------------------------------------------------------------------------------------
  2204. #pragma segment ASelCommand
  2205.  
  2206. void  TZoomInCommand::IZoomInCommand(TIconDocument* itsIconDocument)
  2207. {
  2208.       fIconDocument = itsIconDocument;            // Save a reference to the icon document.
  2209.  
  2210.     this->ICommand(cZoomIn,                        // Initialize the inherited data…
  2211.                    itsIconDocument, 
  2212.                    kCanUndo, 
  2213.                    kCausesChange, 
  2214.                    itsIconDocument);            // …no view or scroller to track.
  2215. } //TZoomInCommand::IZoomInCommand 
  2216.  
  2217. //----------------------------------------------------------------------------------------
  2218. // TZoomInCommand::MakeAppleEvent() 
  2219. //----------------------------------------------------------------------------------------
  2220. TAppleEvent * TZoomInCommand::MakeAppleEvent()
  2221. {
  2222.     CTempDesc    directObjectDesc;
  2223.     TAppleEvent * theZoomInAppleEvent = new TAppleEvent;
  2224.     
  2225.     theZoomInAppleEvent->IAppleEvent(kAEIconEditClass, kAEZoomInID,gServerAddress, kAENoReply);
  2226.     fIconDocument->MakeObjectSpecifier(directObjectDesc, fIconDocument->GetSpecifierForm());
  2227.     theZoomInAppleEvent->WriteParameter(keyDirectObject, directObjectDesc);
  2228.     
  2229.     return theZoomInAppleEvent;
  2230. } //TZoomInCommand::MakeAppleEvent
  2231.  
  2232. //----------------------------------------------------------------------------------------
  2233. // TZoomInCommand::DoIt: 
  2234. //----------------------------------------------------------------------------------------
  2235. #pragma segment ADoCommand
  2236.  
  2237. void TZoomInCommand::DoIt()
  2238. {   
  2239.     short    currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2240.     fIconDocument->GetIconView()->SetMagnification(currentMagnification + 2);
  2241. } // TZoomInCommand::DoIt 
  2242.  
  2243.  
  2244. //----------------------------------------------------------------------------------------
  2245. // TZoomInCommand::UndoIt: 
  2246. //----------------------------------------------------------------------------------------
  2247. #pragma segment ADoCommand
  2248.  
  2249. void TZoomInCommand::UndoIt()
  2250. {
  2251.     short    currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2252.     fIconDocument->GetIconView()->SetMagnification(currentMagnification - 2);
  2253. } // TZoomInCommand::UndoIt 
  2254.  
  2255.  
  2256. //----------------------------------------------------------------------------------------
  2257. // TZoomInCommand::RedoIt: 
  2258. //----------------------------------------------------------------------------------------
  2259. #pragma segment ADoCommand
  2260.  
  2261. void TZoomInCommand::RedoIt()
  2262. {
  2263.      short    currentMagnification = fIconDocument->GetIconView()->GetMagnification();
  2264.     fIconDocument->GetIconView()->SetMagnification(currentMagnification + 2);
  2265. }  // TZoomInCommand::RedoIt 
  2266.  
  2267. //----------------------------------------------------------------------------------------
  2268. // End of UIconEdit.cp
  2269.  
  2270. #pragma segment Inline
  2271.